<template>
  <!--  编辑/新增 弹窗  -->
  <BasicModal  v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
    <Row>
      <slot name="formHeader">任务内容</slot>
      <Tinymce v-model="contentValue" @change="handleContentChange" width="100%" />
    </Row>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { Tinymce } from '/@/components/Tinymce/index';
  import { formSchema } from './schema.data';
  import {  Row } from 'ant-design-vue';

  import { TaskCenterAdd } from '/@/api/taskCenter/taskCenter';
  export default defineComponent({
    name: 'DeptModal',
    components: { BasicModal, BasicForm ,Tinymce,Row},
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      let contentValue = ref('');

      const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
        labelWidth: 130,
        schemas: formSchema,
        showActionButtonGroup: false,
      });

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        resetFields();
        setModalProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        if (unref(isUpdate)) {
          setFieldsValue({
            ...data.record,
          });
          contentValue.value = data.record.content
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));

      let taskContent = ''
      function handleContentChange(value: string) {
        taskContent = value;
      }

      async function handleSubmit() {
        try {
          const values = await validate();
          setModalProps({ confirmLoading: true });
          await TaskCenterAdd({ ...values,content:taskContent });
          closeModal();
          emit('success');
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }



      return { registerModal, registerForm, getTitle, handleSubmit,contentValue,handleContentChange };
    },
  });
</script>
